package com.sugon.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.sugon.dao.DevMaintenRepairDao;
import com.sugon.entity.DevMaintenRepairEntity;
import com.sugon.entity.SparePartConsumRecordEntity;
import com.sugon.service.DevMaintenRepairService;
import com.sugon.service.SparePartConsumRecordService;
import com.sugon.utils.Constant;
import com.sugon.utils.ShiroUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 维修记录单Service实现类
 *
 * @author sugon
 * @date 2019-07-18 11:39:51
 */
@Service("devMaintenRepairService")
public class DevMaintenRepairServiceImpl implements DevMaintenRepairService {

    @Resource
    private DevMaintenRepairDao devMaintenRepairDao;
    @Resource
    private SparePartConsumRecordService sparePartConsumRecordService;

    @Override
    public DevMaintenRepairEntity queryObject(String id) {
        return devMaintenRepairDao.queryObject(id);
    }

    @Override
    public List<DevMaintenRepairEntity> queryList(Map<String, Object> map) {
        return devMaintenRepairDao.queryList(map);
    }

    @Override
    public int queryTotal(Map<String, Object> map) {
        return devMaintenRepairDao.queryTotal(map);
    }

    @Override
    public int save(DevMaintenRepairEntity devMaintenRepair) {
        devMaintenRepair.setId(RandomUtil.randomString(32));
        if (devMaintenRepair.getDelFlag() == null) {
            devMaintenRepair.setDelFlag(Constant.IS_NOT_DEL_INT);
        }
        // 默认自己维修
        if (devMaintenRepair.getOrderNo() == null) {
            devMaintenRepair.setOrderNo(1);
        }
        devMaintenRepair.setGmtCreate(DateUtil.date());
        // 如果未设置创建人员，那么默认为当前系统用户
        if (StrUtil.isEmpty(devMaintenRepair.getCreateBy())) {
            devMaintenRepair.setCreateBy(ShiroUtils.getUserRealName());
        }
        return devMaintenRepairDao.save(devMaintenRepair);
    }

    @Override
    public int update(DevMaintenRepairEntity devMaintenRepair) {
        if (StrUtil.isEmpty(devMaintenRepair.getUpdateBy())) {
            devMaintenRepair.setUpdateBy(ShiroUtils.getUserRealName());
        }
        devMaintenRepair.setUpdateTime(DateUtil.date());
        return devMaintenRepairDao.update(devMaintenRepair);
    }

    @Override
    public int delete(String id) {
        // 如果有换件记录的话，删除下
        Map<String, Object> param = MapUtil.newHashMap();
        param.put("type", "REPAIR");
        param.put("businessId", id);
        List<SparePartConsumRecordEntity> consumeRecords = sparePartConsumRecordService.queryList(param);
        if (CollectionUtil.isNotEmpty(consumeRecords)) {
            for (SparePartConsumRecordEntity consumeRecord : consumeRecords) {
                sparePartConsumRecordService.delete(consumeRecord.getId());
            }
        }
        // 删除维修记录
        return devMaintenRepairDao.delete(id);
    }

    @Override
    public int deleteBatch(String[] ids) {
        int rows = 0;
        for (String id : ids) {
            rows += delete(id);
        }
        return rows;
    }

}
